<?php
    function getTree($data,$cate_ids)
    {
        $html = '';
        foreach($data as $k => $v)
        {
            if(isset($v['cate_child']))
            {   //父亲找到儿子
                $checked = '';
                $html .= '<li><a tname="cate_ids" tvalue="'.$v['cate_id'].'" '.$checked.'>'.$v['cate_name'].'</a>';
                $html .= getTree($v['cate_child'],$cate_ids);
                $html .= '</li>';
            }else{
                $checked = in_array($v['cate_id'],$cate_ids) ? 'checked="true"':'';
                $html .= '<li><a tname="cate_ids" tvalue="'.$v['cate_id'].'" '.$checked.'>'.$v['cate_name'].'</a></li>';
            }
        }
        return '<ul>'.$html.'</ul>';
    }

    $parent_array = array();//记录每一个父节点的地址引用
    $tree_array = array();//整颗树的数组

    foreach($menu_arr as $v){

        $node_num = ($v['right_value']-$v['left_value'] - 1)/2;

        if($v['parent_cate_id']){//如果没有parent_cate_id则是顶层

            $parent_array[$v['parent_cate_id']]['cate_child'][$v['cate_id']] = $v;

            if($node_num > 0){

                $parent_array[$v['cate_id']] = &$parent_array[$v['parent_cate_id']]['cate_child'][$v['cate_id']];
            }

        }else{
            $tree_array[$v['cate_id']] = $v;

            if($node_num > 0){

                $parent_array[$v['cate_id']] = &$tree_array[$v['cate_id']];
            }
        }
    }
?>
<div class="pageContent">
	<div class="pageFormContent" layoutH="60">
        <ul class="tree treeFolder treeCheck expand" id="_select_cate">
            <li><a tname="cate_ids" tvalue="0">管理中心</a>
                <?php echo getTree($tree_array,$cate_ids);?>
            </li>
        </ul>
	</div>
	<div class="formBar">
		<ul>
			<li><a class="button"  href="javascript:void(0);" onclick="userTreeCallBack();"><span>确认</span></a></li>
			<li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭</button></div></div></li>
		</ul>
	</div>
</div>
<script type="text/javascript">

function userTreeCallBack(){
    
    var dialog = $("body").data('user_cate_tree');
    var el = dialog.find("div.ckbox.checked,div.ckbox.indeterminate");

    var ids = [];
    el.each(function(){
        ids.push($(this).children(':checkbox').val());
    });
    $.post('<?php echo site_url('/public/aj_set_user_role.php')?>',{cate_ids:ids,user_id:<?php echo $user_id?>},function(data){
        if(data.statusCode == 200){
            alertMsg.correct(data.message);
        }else{
            alertMsg.error(data.message);
        }
    });
}
</script>